package jsu.xxy.coffee3.Utils;

import io.jsonwebtoken.*;

import java.util.Date;
import java.util.UUID;

public class JwtUtil {
    private static long time = 1000*60*24*60;
    private static String signature = "admin";
    public static String creatToken(String phone, int i){
        JwtBuilder jwtBuilder = Jwts.builder();
        String jwtToken = jwtBuilder
//                head
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")
//                payload
                .claim("username",phone)
                .claim("role",i)
                .setSubject("test")//主题
                .setExpiration(new Date(System.currentTimeMillis()+time))//有效时间
                .setId(UUID.randomUUID().toString())
//                signature
                .signWith(SignatureAlgorithm.HS256,signature)
                .compact();
        System.out.println(jwtToken);
        return jwtToken;
    }
    public static boolean checkToken(String token){
        if(token == null){
            return false;
        }
        try {
            JwtParser jwtParser = Jwts.parser();
            Jws<Claims> claimsJws = jwtParser.setSigningKey(signature).parseClaimsJws(token);
        }catch (Exception e){
            return false;
        }
        return true;
    }
}
